package com.example.emergencysystem_user;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import android.graphics.Bitmap;
import android.provider.MediaStore.Video;
import android.util.Log;

public class TCPclient implements Runnable {
	private String name;
	private String phone;
	private String address;
	private String report;
	private String reportContent;
	private String myInfoPic = "picture";
	private String path;
	private Double latitdue;
	private Double longitude;
	DataInputStream dis;
	DataOutputStream dos;
	byte[] buf = new byte[10000];

	public TCPclient(String name, String phone, String address, String report,
			String reportContent, String path, Double latitude, Double longitude) {
		this.name = name;
		this.phone = phone;
		this.address = address;
		this.report = report;
		this.reportContent = reportContent;
		this.path = path;
		this.latitdue = latitude;
		this.longitude = longitude;
	}

	public void run() {
		try {

			Log.d("TCP", "C: Connecting...");
			Socket socket = new Socket("192.168.0.4", 8080);
			try {
				
				BufferedReader in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));
				String KEY = in.readLine();
				Log.d("TCP", "C: " + KEY);
				if (Integer.parseInt(KEY) == 1) {
					dos = new DataOutputStream(
							socket.getOutputStream());
					dis = new DataInputStream(
							new FileInputStream(new File(path)));
					while (dis.read(buf) > 0) {
						dos.write(buf);
						dos.flush();

					}
					dos.close();
					in.close();

				}

				Log.d("TCP", "C: Sending: '" + name + "'");
				Log.d("TCP", "C: Sending: '" + phone + "'");
				Log.d("TCP", "C: Sending: '" + address + "'");
				Log.d("TCP", "C: Sending: '" + report + "'");
				Log.d("TCP", "C: Sending: '" + reportContent + "'");
				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())), true);

				out.println(name + "@" + phone + "@" + address + "@" + report
						+ "@" + reportContent + "@" + latitdue.toString() + "@"
						+ longitude.toString());
				Log.d("TCP", "C: Sent.");
				Log.d("TCP", "C: Done.");
				out.flush();

				Log.d("TCP", "C: Sending InfoImage");
				dis = new DataInputStream(new FileInputStream(new File(
						"mnt/sdcard/EmergencySystem/img/picture.jpg")));
				DataOutputStream dos = new DataOutputStream(
						socket.getOutputStream());

				while (dis.read(buf) > 0) {
					dos.write(buf);
					dos.flush();
				}
				Log.d("TCP", "C: Sending InfoImage Success");
				//dos2.close();
				dos.close();

			} catch (Exception e) {
				Log.e("TCP", "C: Error1", e);

			} finally {
				socket.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("TCP", "C: Error2", e);
		}
	}
}